package net.child.chess.utils;

public class Position 
{
	public Position(int position)
	{
		position(position);
	}
	public Position(int x, int y)
	{
		position(x, y);
	}
	public Position(Position pos)
	{
		position(pos);
	}
	
	public void position(Position pos)
	{
		mX = pos.mX;
		mY = pos.mY;
		mPosition = pos.mPosition;
	}
	
	public int x()
	{
		return mX;
	}
	public int y()
	{
		return mY;
	}
	public Position position(int x, int y)
	{
		mX = x;
		mY = y;
		mPosition = mX + (mY * ChessDef.BOARD_NUM_COLUMNS);
		return this;
	}
	
	public int position()
	{
		return mPosition;
	}
	public void position(int position)
	{
		mX = position % ChessDef.BOARD_NUM_COLUMNS;
		mY = position / ChessDef.BOARD_NUM_ROWS;
		mPosition = position; 
	}
	
	
	public boolean decX()
	{
		//	Is this the first column?
		if(mX == 0)
		{
			return false;
		}
		--mX;
		--mPosition;
		return true;
	}
	public boolean incX()
	{
		//	Is this the last column?
		if(mX == (ChessDef.BOARD_NUM_COLUMNS - 1))
		{
			return false;
		}
		++mX;
		++mPosition;
		return true;
	}
	
	public boolean decY()
	{
		//	Is this is the first row?
		if(mY == 0)
		{
			return false;
		}
		--mY;
		mPosition -= ChessDef.BOARD_NUM_COLUMNS;
		return true;
	}
	public boolean incY()
	{
		//	Is this is the last row?
		if(mY == (ChessDef.BOARD_NUM_ROWS -1))
		{
			return false;
		}
		++mY;
		mPosition += ChessDef.BOARD_NUM_COLUMNS;
		return true;
	}
	
	public boolean isValid()
	{
		return ((mX >= 0) && (mY >= 0) && (mX < ChessDef.BOARD_NUM_COLUMNS) && (mY < ChessDef.BOARD_NUM_ROWS));
	}
	
	private int mX;
	private int mY;
	private int mPosition;
}
